# daos/charging_station_dao.py
import asyncpg
from typing import List, Optional
from models.charging_station import ChargingStationDB
from config import settings

class ChargingStationDAO:
    def __init__(self, connection: asyncpg.Connection):
        self.conn = connection
        self.table_name = settings.TABLE_NAME_CHARGING_STATIONS

    async def get_all(self) -> List[ChargingStationDB]:
        """获取所有充电站"""
        query = f"""
        SELECT id, amap_id, name, address, tel, province, city, district, 
               business_area, operator, charger_type, 
               ST_AsText(location) as location,
               created_at, updated_at 
        FROM {self.table_name}
        ORDER BY created_at DESC
        """
        rows = await self.conn.fetch(query)
        return [ChargingStationDB(**dict(row)) for row in rows]

    async def get_by_id(self, station_id: str) -> Optional[ChargingStationDB]:
        """根据ID获取单个充电站"""
        query = f"""
        SELECT id, amap_id, name, address, tel, province, city, district, 
               business_area, operator, charger_type, 
               ST_AsText(location) as location,
               created_at, updated_at 
        FROM {self.table_name} 
        WHERE id = $1
        """
        row = await self.conn.fetchrow(query, station_id)
        if row:
            return ChargingStationDB(**dict(row))
        return None

    # 可以添加 create, update, delete 等方法
    # async def create(self, station: ChargingStationCreate) -> ChargingStationDB:
    #     ...